$(document).ready(function() {
    var str="";
    $.ajax({
        type : "get",
        url : "/register/getClazz",
        dataType : "json",
        success : function(data) {
            for(var i in data){
                if(i==0){
                    $("#selectClazz").append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
                }else{
                    $("#selectClazz").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                }
            }
            console.log(data);
        },
        error : function(xhr,textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });
    $("#tdate").change(function(){
        $("#tdate").attr("value",$(this).val()); //赋值
    });
$("#questionBank").click(function(){
    $("#t1 tr:not(:first)").empty();
    $.ajax({
        type : "get",
        url : "/teamExam/getQuestion",
        dataType : "json",
        data:{
            pageNo:1
        },
        success : function(data) {
            for(var i in data){
                $("#t1").append("<tr><td style='display: none'>" + data[i].id+ "</td>" +
                    "<td>"+data[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
            }
           $("#currentPage").html("1");
            if(1==parseInt($("#currentPage").html().toString())){
                $("#previousPage").addClass("disabled");
            }
            submitQuestion();
            console.log(data);
        },
        error : function(xhr,textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });
});

    $("#homePage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#previousPage").addClass("disabled");
        $("#nextPage").removeClass("disabled");
        $.ajax({
            type : "get",
            url : "/teamExam/getQuestion",
            dataType : "json",
            data:{
                pageNo:1
            },
            success : function(data) {
                for(var i in data){
                    $("#t1").append("<tr><td style='display: none'>" + data[i].id+ "</td>" +
                        "<td>"+data[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
                }
                submitQuestion();
                $("#currentPage").html("1");
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#endPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#previousPage").removeClass("disabled");
        $("#nextPage").addClass("disabled");
            $.ajax({
                type : "get",
                url : "/teamExam/getEndPage",
                dataType : "json",
                success : function(data) {
                    for(var i in data.sumData){
                        $("#t1").append("<tr><td style='display: none'>" + data.sumData[i].id+ "</td>" +
                            "<td>"+data.sumData[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
                    }
                    submitQuestion();
                    currentPage=$("#currentPage").html(data.pageNo);
                    console.log(data);
                },
                error : function(xhr,textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
    });
    $("#previousPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#nextPage").removeClass("disabled");
        var currentPage=$("#currentPage").html();
        var pageNo=parseInt(currentPage.toString())-1;
        $.ajax({
            type : "get",
            url : "/teamExam/getPreviousPage",
            dataType : "json",
            data:{
                currentPage:pageNo
            },
            success : function(data) {
                for(var i in data){
                    $("#t1").append("<tr><td style='display: none'>" + data[i].id+ "</td>" +
                        "<td>"+data[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
                }
                submitQuestion();
                $("#currentPage").html(pageNo);
                if(1==pageNo){
                    $("#previousPage").addClass("disabled");
                }else{
                    $("#previousPage").removeClass("disabled");
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#nextPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        $("#previousPage").remove("disabled");
        var currentPage=$("#currentPage").html();
        var pageNo=parseInt(currentPage.toString())+1;
        $.ajax({
            type : "get",
            url : "/teamExam/getNextPage",
            dataType : "json",
            data:{
                currentPage:pageNo
            },
            success : function(data) {
                for(var i in data.sumData){
                    $("#t1").append("<tr><td style='display: none'>" + data.sumData[i].id+ "</td>" +
                        "<td>"+data.sumData[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
                }
                submitQuestion();
                $("#currentPage").html(pageNo);
                if(data.pageNo==pageNo){
                    $("#nextPage").addClass("disabled");
                }else{
                    $("#previousPage").removeClass("disabled");
                    $("#nextPage").removeClass("disabled");
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#skipPage").click(function(){
        $("#t1 tr:not(:first)").empty();
        var currentPage=$("#text").val();
        var pageNo=parseInt(currentPage.toString());
        if(isNaN(pageNo)){
            alert("请输入数字");
        }
        $.ajax({
            type : "get",
            url : "/teamExam/getNextPage",
            dataType : "json",
            data:{
                currentPage:pageNo
            },
            success : function(data) {
                for(var i in data.sumData){
                    $("#t1").append("<tr><td style='display: none'>" + data.sumData[i].id+ "</td>" +
                        "<td>"+data.sumData[i].content+"</td><td >\<input type='button' class='chooseQuestion' value='添加'>\</td></tr>");
                }
                submitQuestion();
                $("#currentPage").html(pageNo);
                if(data.pageNo<pageNo){
                    alert("输入页数超出范围,请重新输入!");
                }
                if(1==pageNo){
                    $("#previousPage").addClass("disabled");
                    $("#nextPage").removeClass("disabled");
                    return;
                }
                if(data.pageNo==pageNo){
                    $("#nextPage").addClass("disabled");
                    $("#previousPage").removeClass("disabled");
                    return;
                }
                else{
                    $("#previousPage").removeClass("disabled");
                    $("#nextPage").removeClass("disabled");
                    return;
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#addQuestion").click(function(){
        var content=$("#question").val();
        $.ajax({
            type : "get",
            url : "/teamExam/addQuestion",
            dataType : "text",
            data:{
                content:content
            },
            success : function(data) {
                $("#question").val("");
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });
    function  submitQuestion(){
        $(".chooseQuestion").click(function(){
            var id=$(this).parent().parent().find("td").eq(0).text();
            var content=$(this).parent().parent().find("td").eq(1).text();
            $("#tques").append("<tr><td style='display: none'>" + id+ "</td>" +
                "<td>"+content+"</td><td >\<input type='button' class='deleteQuestion' value='移除'>\</td></tr>");
            $(".deleteQuestion").click(function(){
                $(this).parent().parent().remove();
            });
        });

        $("#refer").click(function(){
            var tableId = document.getElementById("tques");
            var trs = tableId.rows;
            var clazzID=$("#selectClazz").val();
            if (trs.length == 1) {
                alert("提交内容为空!!");
                return;
            }
            if($("#tdate").val()==""||$("#tdate").val()==null)
            {
                alert("请选择日期");
                return;
            }
            for(var i = 1, len = trs.length ; i < len; i++) {
                var id = trs[i].cells[0].innerText;
                str += id + ",";
            }
            var date=$("#tdate").val();
            $.ajax({
                type : "POST",
                url : "/teamExam/referQuestion",
                dataType : "text",
                data:{
                    questionId:str,
                    date:date,
                    clazzId:clazzID
                },
                success : function(data) {
                    $("#tques tr:not(:first)").empty();
                    console.log(data);
                },
                error : function(xhr,textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
        });
    }

});